﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using FluentValidation.Results;
using STLGROUP.Layers.Common;

namespace STLGROUP.Layers.DataAccessLayers.Validators
{
    public class DT_NhanVienValidator : AbstractValidator<DT_NhanVien>
    {
        public DT_NhanVienValidator()
        {
            RuleFor(employee => employee.HoVaTenDem).NotNull().WithMessage(Const.VALD_PROPERTY_KHONG_BO_TRONG).WithPropertyName("Họ và tên đệm");
            RuleFor(employee => employee.TenNhanVien).NotNull().WithMessage(Const.VALD_PROPERTY_KHONG_BO_TRONG).WithPropertyName("Tên nhân viên");
            RuleFor(employee => employee.SoCMND).NotNull().WithMessage(Const.VALD_PROPERTY_KHONG_BO_TRONG).WithPropertyName("Số CMND");
            RuleFor(employee => employee.IDPhongBan).NotEmpty().WithMessage(Const.VALD_PROPERTY_KHONG_BO_TRONG).WithPropertyName("Phòng ban");
            RuleFor(employee => employee.IDChucVu).NotEmpty().WithMessage(Const.VALD_PROPERTY_KHONG_BO_TRONG).WithPropertyName("Chức vụ");
            RuleFor(employee => employee.IDLoaiNhanVien).NotEmpty().WithMessage(Const.VALD_PROPERTY_KHONG_BO_TRONG).WithPropertyName("Loại nhân viên");
            RuleFor(employee => employee.IDTinhThanh).NotEmpty().WithMessage(Const.VALD_PROPERTY_KHONG_BO_TRONG).WithPropertyName("Tỉnh thành");
            RuleFor(employee => employee.DiaChi).NotNull().WithMessage(Const.VALD_PROPERTY_KHONG_BO_TRONG).WithPropertyName("Địa chỉ");
            RuleFor(employee => employee.Email).NotNull().WithMessage(Const.VALD_PROPERTY_KHONG_BO_TRONG).WithPropertyName("Email");            
            RuleFor(employee => employee.SoDienThoai).NotNull().WithMessage(Const.VALD_PROPERTY_KHONG_BO_TRONG).WithPropertyName("Số điện thoại");

            RuleFor(employee => employee.SoCMND).Must(isNumber).When(employee => !string.IsNullOrEmpty(employee.SoCMND)).WithPropertyName("Số CMND").WithMessage(Const.VALD_PHAI_LA_CHU_SO);            

            //RuleFor(employee => employee.Email).EmailAddress().When(employee => !string.IsNullOrEmpty(employee.Email)).WithMessage(Const.VALD_KHONG_HOP_LE).WithPropertyName("Email");
            RuleFor(employee => employee.SoDienThoai).Must(isNumber).When(employee => !string.IsNullOrEmpty(employee.SoDienThoai)).WithPropertyName("Số điện thoại").WithMessage(Const.VALD_PHAI_LA_CHU_SO);
            RuleFor(employee => employee.Fax).Must(isNumber).When(employee => !string.IsNullOrWhiteSpace(employee.Fax)).WithPropertyName("Fax").WithMessage(Const.VALD_PHAI_LA_CHU_SO); 
            
        }

        private bool isNumber(string str)
        {
            str = str.Trim();
            long a;
            return long.TryParse(str, out a);
        }
    }
}
